import { CHANGE_HOME_DATA , LOAD_LIST, SHOW_BACK} from './actionTypes'
import axios from 'axios'

const changeHomeData = (data) => ({
  type: CHANGE_HOME_DATA,
  topicList: data.topicList,
  articleList:  data.articleList,
  recommendList:  data.recommendList
});

const LoadList = (data) => ({
  type: LOAD_LIST,
  page: data.page,
  list: data.list
});

export const changeHomeAction = () => {
  return (dispatch) => {
    axios.get('/api/home.json').then(res => {
       dispatch(changeHomeData(res.data))
    }).catch(err => {
      console.log(err)
    })
  }
};

export const loadListAction = (page) => {
  return (dispatch) => {
   axios.get('/api/List.json?'+ page).then((res) => {
     console.log(res.data)
     let data = {
       page: page + 1,
       list: res.data.list
     };
     dispatch(LoadList(data))
   }).catch(err => {
     console.log(err)
   })
  }
};

export const showBack = (bool) => ({
  type: SHOW_BACK,
  bool: bool
});